package com.zx.mobile.service.proxy;

import com.zx.map.dao.model.MerchantCertificateInfo;
import com.zx.map.dao.model.StoreInfo;
import com.zx.map.service.model.MerchantInfoModel;
import com.zx.map.service.model.MerchantInfoTodoTask;
import com.zx.map.service.model.SettleCardInfoModel;
import com.zx.map.service.model.TodoTaskModel;
import com.zx.mobile.MobileCommonHelper;
import com.zx.mobile.domain.BindcardNewRequest;
import com.zx.platform.utils.BankCardUtils;
import com.zx.platform.utils.IDCardUtils;
import com.zx.scp.service.ImageCommonHelper;
import com.zx.scp.service.model.ImageSettleResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Objects;

public class BindCardCommonNewProxy {
    /**
     * log
     */
    private static Logger log = Logger.getLogger(BindCardCommonNewProxy.class);

    public static String checkStoreInfo(StoreInfo storeInfo) {
    	 if (!IDCardUtils.validateCard(storeInfo.getIdentifyNo())) { //身份证校验
             return "身份证校验未通过";
         }
		return null;
    	
    }
    public static String checkBindCard(BindcardNewRequest bindcardNewRequest) {

        if (StringUtils.isBlank(bindcardNewRequest.getUserName())) {
            return "输入项必填";
        }
        if (StringUtils.isBlank(bindcardNewRequest.getAccountNo())) {
            return "输入项必填";
        } else if (!BankCardUtils.checkBankCard(bindcardNewRequest.getAccountNo())) { //银行卡校验
            return "银行卡校验未通过";
        }
        if (StringUtils.isBlank(bindcardNewRequest.getAppUserId())) {
            return "输入项必填";
        }
        if (StringUtils.isBlank(bindcardNewRequest.getBankCode())) {
            return "输入项必填";
        }
//		if (StringUtils.isBlank(bindcardNewRequest.getBranchName())) {
//			return "输入项必填";
//		}
        if (StringUtils.isBlank(bindcardNewRequest.getHoldIndentityCard())) {
            return "输入项必填";
        }
        if (StringUtils.isBlank(bindcardNewRequest.getLegalIndentityImg())) {
            return "输入项必填";
        }
        if (StringUtils.isBlank(bindcardNewRequest.getIdentityNo())) {
            return "输入项必填";
        } else if (!IDCardUtils.validateCard(bindcardNewRequest.getIdentityNo())) { //身份证校验
            return "身份证校验未通过";
        }
        return null;
    }

    /**
     * 添加请求信息
     *
     * @param bindcardNewRequest
     * @param request
     * @return
     */
    public static MerchantInfoTodoTask addBindCardRequest(
            StoreInfo storeInfo,MerchantInfoModel merchantModel, HttpServletRequest request) {
        MerchantInfoTodoTask todoTask = new MerchantInfoTodoTask();
        // 1、添加结算卡信息
        SettleCardInfoModel settleCardInfoModel = new SettleCardInfoModel();
        settleCardInfoModel.setBankAccount(storeInfo.getSettlementAccount());
        settleCardInfoModel.setBankUsrname(storeInfo.getRealName());
        settleCardInfoModel.setDepositBank(storeInfo.getSettlementBank());
        todoTask.setSettleCardInfoModel(settleCardInfoModel);

        // 2、添加证件信息
        MerchantCertificateInfo record = new MerchantCertificateInfo();
//        try {
//            ImageSettleResponse holdCard = ImageCommonHelper.getImageResponse(bindcardNewRequest.getHoldIndentityCard(),merchantModel.getSysAccount());
//            if ("0000".equals(holdCard.getReturnCode())) {
//                record.setHoldIndentityCard(holdCard.getImageKey());
//            } else {
//                throw new RuntimeException("图片处理异常");
//            }
//            ImageSettleResponse identityCard = ImageCommonHelper.getImageResponse(bindcardNewRequest.getLegalIndentityImg(),merchantModel.getSysAccount());
//            if ("0000".equals(identityCard.getReturnCode())) {
//                record.setLegalIndentityImg(identityCard.getImageKey());
//            } else {
//                throw new RuntimeException("图片处理异常");
//            }
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
        record.setBussinessImg(storeInfo.getBusinessLicenseImg());//营业执照
        record.setHoldIndentityCard(storeInfo.getHoldLicenseImg());//手持营业执照
        record.setLegalIndentityImg(storeInfo.getIdPositiveImg());//身份正面照
        record.setLegalOppindentityImg(storeInfo.getIdNegativeImg());//身份证反面
        
        record.setMerchantId(storeInfo.getMerchantId());
        record.setCreateDate(new Date());
        todoTask.setMerchantCertificateInfo(record);

        // 3、设置待处理事项内容
        TodoTaskModel todoTaskModel = new TodoTaskModel();
        if(Objects.nonNull(storeInfo.getTodoTaskId())) {
        	todoTaskModel.setTaskId(storeInfo.getTodoTaskId());
        }
        todoTaskModel.setApplyType("");
        todoTaskModel.setApproveStatus(MobileCommonHelper.APPROVESTATUS_WAIT);
        todoTaskModel.setTaskType(MobileCommonHelper.TASKTYPE_MERCHANTADD);  ////新增商户
        todoTaskModel.setTaskOperatorType(MobileCommonHelper.TASKOPERATORTYPE_UPDATE);//修改
        todoTaskModel.setTaskKey(storeInfo.getMerchantId());//主键信息
        todoTask.setTodoTaskModel(todoTaskModel);
        return todoTask;
    }
}
